package com.wyzz.hr.action;

import java.util.*;
import org.apache.axis.utils.StringUtils;
import org.apache.log4j.Logger;

import com.wyzz.hr.base.BaseAction;
import com.wyzz.hr.base.DWRUtil;
import com.wyzz.hr.bo.IAttdMachineBO;
import com.wyzz.hr.bo.IAttdSyncRecordBO;
import com.wyzz.hr.bo.IAttdoriginaldataBO;
import com.wyzz.hr.bo.IEmployeeBo;
import com.wyzz.hr.bo.IMachineBO;
import com.wyzz.hr.bo.MachineBOFactory;
import com.wyzz.hr.pojo.AttdMachine;
import com.wyzz.hr.pojo.AttdSyncRecord;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.StringUtil;

public class DwrSyncAttdMachine extends BaseAction {

	public DwrSyncAttdMachine() {
		msgNoAuth = "\u60A8\u65E0\u6743\u6267\u884C\u6B64\u64CD\u4F5C\uFF0C\u8BF7\u91CD\u65B0\u767B\u9646\uFF01";
		msgNoEmpChoosed = "\u8BF7\u60A8\u9009\u62E9\u8981\u540C\u6B65\u7684\u7528\u6237\uFF01";
		msgNoMacChoosed = "\u8BF7\u9009\u62E9\u8003\u52E4\u673A\uFF01";
		msgParamError = "\u53C2\u6570\u9519\u8BEF\uFF0C\u8BF7\u5237\u65B0\u540E\u91CD\u8BD5\uFF01";
		msgSuccMsgStart = "\u4EE5\u4E0B\u8BB0\u5F55\u540C\u6B65\u6210\u529F\uFF1A\n";
		msgErrorMsgStart = "\u4EE5\u4E0B\u8BB0\u5F55\u540C\u6B65\u51FA\u9519\uFF1A\n";
		msgConnFail = "{0}\u8FDE\u63A5\u5931\u8D25\uFF1A{1}\n";
		msgSyncEmpStartLog = "{0}|\u5411\u8003\u52E4\u673A\u4E2D\u540C\u6B65\u5458\u5DE5\u4FE1\u606F";
		msgSyncEmpEndLog = "\u5411\u8003\u52E4\u673A\u4E2D\u540C\u6B65\u5458\u5DE5\u4FE1\u606F\u6761\u6570(Set employee records)\uFF1A{0}!";
		msgEmpCardNoNull = "{0} | {1} | \u8003\u52E4\u673A:{2} | \u8BE5\u5458\u5DE5\u5361\u53F7\u4E3A\u7A7A\uFF01\n";
		msgEmpCardNoRepeat = "{0} | {1} | \u8003\u52E4\u673A:{2} | \u8BE5\u5458\u5DE5\u5361\u53F7\u5B58\u5728\u91CD\u590D\uFF01\n";
		msgEmpSyncFail = "{0} | {1} | \u8003\u52E4\u673A:{2} | \u540C\u6B65\u5931\u8D25\uFF0C{3}\n";
		msgEmpSyncSucc = "{0} | {1} | \u8003\u52E4\u673A:{2} | \u540C\u6B65\u6210\u529F\uFF01\n";
		msgGetEmpStartLog = "{0} | \u540C\u6B65\u8003\u52E4\u673A\u4E2D\u7684\u5458\u5DE5\u4FE1\u606F";
		msgGetEmpEndLog = "\u540C\u6B65\u8003\u52E4\u673A\u4E2D\u5458\u5DE5\u4FE1\u606F(Get employee records)\uFF1A{0}!";
		msgEmpDeleteFail = "{0} | \u8003\u52E4\u673A:{1} | \u5220\u9664\u5931\u8D25\uFF0C{2}\n";
		msgEmpDeleteSucc = "\u8003\u52E4\u673A{0}\u540C\u6B65\u6210\u529F\u3002\n";
		msgNoCardDataToRead = "\u6CA1\u6709\u9700\u8981\u8BFB\u53D6\u7684\u8003\u52E4\u673A\u540C\u6B65\u8BB0\u5F55\uFF01";
		msgNoRecChoosed = "\u8BF7\u9009\u62E9\u8981\u5728\u8003\u52E4\u673A\u4E2D\u5220\u9664\u7684\u8BB0\u5F55\uFF01";
		msgDeleteEmpStartLog = "{0} | \u6279\u91CF\u5220\u9664\u8003\u52E4\u673A\u4E2D\u5458\u5DE5\u4FE1\u606F";
		msgDeleteEmpEndLog = "\u6279\u91CF\u5220\u9664\u8003\u52E4\u673A\u4E2D\u5458\u5DE5\u4FE1\u606F\u6761\u6570(Delete employee records)\uFF1A{0}\uFF01";
		msgDeleteEmpFail = "{0} | {1} \u5220\u9664\u5931\u8D25\uFF1A{2}\n";
		msgDeleteEmpSucc = "{0} | {1} \u5220\u9664\u6210\u529F\uFF01\n";
		msgMacClass = "\u8003\u52E4\u673A\u7C7B\u578B\u4E0D\u80FD\u4E3A\u7A7A\uFF01";
		msgMacIp = "\u8003\u52E4\u673AIP\u4E0D\u80FD\u4E3A\u7A7A\uFF01";
		msgMacPort = "\u8003\u52E4\u673A\u63A5\u53E3\u5FC5\u987B\u4E3A\u6570\u5B57\uFF01";
		msgMacPassword = "\u8003\u52E4\u673A\u5BC6\u7801\u5FC5\u987B\u4E3A\u6570\u5B57\uFF01";
		msgCopyOneMachine = "\u9009\u4E2D\u7684\u5458\u5DE5\u5FC5\u987B\u5728\u540C\u4E00\u53F0\u8003\u52E4\u673A\u4E0A\uFF0C\u8BF7\u91CD\u65B0\u9009\u62E9\uFF01";
		msgFromMachineError = "\u62F7\u8D1D\u8D77\u59CB\u8003\u52E4\u673A\u4E0D\u80FD\u5305\u542B\u5728\u76EE\u7684\u8003\u52E4\u673A\u5217\u8868\u4E2D\uFF01";
		msgCopyMachineFinger = "{0}\u4E0D\u662F\u6307\u7EB9\u673A\uFF0C\u4E0D\u80FD\u8FDB\u884C\u6307\u7EB9\u62F7\u8D1D\uFF01";
		msgCopyNoRec = "\u8D77\u59CB\u8003\u52E4\u673A{0}\u4E0A\u65E0\u6570\u636E\u53EF\u4EE5\u62F7\u8D1D\uFF01";
		msgSyncTimeFail = "\u540C\u6B65\u65F6\u95F4\u5931\u8D25\uFF01";
		msgSyncTimeSucc = "\u540C\u6B65\u65F6\u95F4\u6210\u529F\uFF01";
	}

	public String syncToMachine(String empIdStr, String machineIdStr,
			Integer userType, String password) {
		if ("error".equals(DWRUtil.checkAuth("attdSyncRecordShow", "execute")))
			return StringUtil.message(msgNoAuth, new Object[0]);
		if (StringUtils.isEmpty(empIdStr))
			return StringUtil.message(msgNoEmpChoosed, new Object[0]);
		if (StringUtils.isEmpty(machineIdStr))
			return StringUtil.message(msgNoMacChoosed, new Object[0]);
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		IAttdMachineBO attdMachineBO = (IAttdMachineBO) SpringBeanFactory
				.getBean("attdMachineBO");
		IAttdSyncRecordBO attdSyncRecordBO = (IAttdSyncRecordBO) SpringBeanFactory
				.getBean("attdSyncRecordBO");
		String empNoArray[] = empIdStr.split(";");
		List employeeList = employeeBo.getEmpsNeedCard(Integer.valueOf(1),
				empNoArray);
		if (employeeList == null || employeeList.isEmpty())
			return StringUtil.message(msgParamError, new Object[0]);
		String machineIdArray[] = machineIdStr.split(";");
		List attdMachineList = attdMachineBO
				.findAttdMachineByIds(machineIdArray);
		if (attdMachineList == null)
			return StringUtil.message(msgParamError, new Object[0]);
		List macRecList = attdSyncRecordBO
				.getRecListFromMachine(machineIdArray);
		Map macRecMap = new HashMap();
		for (int i = 0; macRecList != null && i < macRecList.size(); i++) {
			AttdSyncRecord rec = (AttdSyncRecord) macRecList.get(i);
			macRecMap
					.put((new StringBuilder()).append(rec.getAsrEmpMachineNo())
							.append(rec.getAsrAttdMachine().getMacId())
							.toString(), rec);
		}

		String returnStr = "";
		String succMsg = msgSuccMsgStart;
		String errorMsg = msgErrorMsgStart;
		List toUpdateRecList = new ArrayList();
		for (Iterator it = attdMachineList.iterator(); it.hasNext();) {
			AttdMachine attdMachine = (AttdMachine) it.next();
			if (!returnStr.equals(""))
				returnStr = (new StringBuilder()).append(returnStr).append("")
						.toString();
			IMachineBO machineBO = MachineBOFactory.getMachineBO(attdMachine
					.getMacClass());
			int errorCode = machineBO.connectNet(attdMachine);
			if (errorCode != machineBO.getSuccCode()) {
				returnStr = (new StringBuilder()).append(returnStr).append(
						StringUtil.message(msgConnFail, new Object[] {
								attdMachine.getMacNo(),
								machineBO.getErrorByCode(errorCode) }))
						.toString();
			} else {
				logger.info(StringUtil.message(msgSyncEmpStartLog,
						new Object[] { getCurrentEmp().getEmpName() }));
				it = employeeList.iterator();
				do {
					if (!it.hasNext())
						break;
					Employee emp = (Employee) it.next();
					if (attdMachine.getMacHasFinger().intValue() == 0) {
						if (StringUtils.isEmpty(emp.getEmpShiftNo())) {
							errorMsg = (new StringBuilder())
									.append(errorMsg)
									.append(
											StringUtil
													.message(
															msgEmpCardNoNull,
															new Object[] {
																	emp
																			.getEmpDistinctNo(),
																	emp
																			.getEmpName(),
																	attdMachine
																			.getMacNo() }))
									.toString();
							continue;
						}
						if (checkCardNoExists(macRecList, emp, attdMachine)) {
							errorMsg = (new StringBuilder())
									.append(errorMsg)
									.append(
											StringUtil
													.message(
															msgEmpCardNoRepeat,
															new Object[] {
																	emp
																			.getEmpDistinctNo(),
																	emp
																			.getEmpName(),
																	attdMachine
																			.getMacNo() }))
									.toString();
							continue;
						}
					}
					AttdSyncRecord record = (AttdSyncRecord) macRecMap
							.get((new StringBuilder()).append(
									emp.getEmpMachineNo()).append(
									attdMachine.getMacId()).toString());
					if (record == null) {
						record = new AttdSyncRecord();
						record.setAsrAttdMachine(attdMachine);
						record.setAsrEmpMachineNo(emp.getEmpMachineNo());
					}
					record.setAsrEmp(emp);
					record.setAsrAuthLevel(userType);
					record.setPassword(password);
					if (!StringUtils.isEmpty(emp.getEmpShiftNo()))
						record.setAsrEmpCardNo(emp.getEmpShiftNo());
					errorCode = machineBO.addOrModifyUser(record);
					if (errorCode != machineBO.getSuccCode()) {
						errorMsg = (new StringBuilder())
								.append(errorMsg)
								.append(
										StringUtil
												.message(
														msgEmpSyncFail,
														new Object[] {
																emp
																		.getEmpDistinctNo(),
																emp
																		.getEmpName(),
																attdMachine
																		.getMacNo(),
																machineBO
																		.getErrorByCode(errorCode) }))
								.toString();
						record.setAsrSync(Integer.valueOf(0));
					} else {
						succMsg = (new StringBuilder()).append(succMsg).append(
								StringUtil.message(msgEmpSyncSucc,
										new Object[] { emp.getEmpDistinctNo(),
												emp.getEmpName(),
												attdMachine.getMacNo() }))
								.toString();
						record.setAsrSync(Integer.valueOf(1));
					}
					toUpdateRecList.add(record);
					macRecList.add(record);
				} while (true);
				machineBO.refreshData();
				machineBO.disConnectNet();
			}
		}

		attdSyncRecordBO.batchAttdSyncRecord(toUpdateRecList);
		if (!succMsg.equals(msgSuccMsgStart))
			returnStr = (new StringBuilder()).append(returnStr).append(succMsg)
					.toString();
		if (!errorMsg.equals(msgErrorMsgStart))
			returnStr = (new StringBuilder()).append(returnStr)
					.append(errorMsg).toString();
		logger.info(StringUtil.message(msgSyncEmpEndLog, new Object[] { Integer
				.valueOf(employeeList.size()) }));
		return returnStr;
	}

	private boolean checkCardNoExists(List recList, Employee emp,
			AttdMachine mac) {
		for (int i = 0; i < recList.size(); i++) {
			AttdSyncRecord rec = (AttdSyncRecord) recList.get(i);
			if (rec.getAsrAttdMachine().getMacId().equals(mac.getMacId())
					&& rec.getAsrEmpCardNo().equals(emp.getEmpShiftNo())
					&& (rec.getAsrEmp() == null || !rec.getAsrEmp().getId()
							.equals(emp.getId())))
				return true;
		}

		return false;
	}

	public String syncToSystem(String machineIdStr, Integer isClean) {
		if ("error".equals(DWRUtil.checkAuth("attdSyncRecordShow", "execute")))
			return StringUtil.message(msgNoAuth, new Object[0]);
		if (StringUtils.isEmpty(machineIdStr))
			return StringUtil.message(msgNoMacChoosed, new Object[0]);
		IEmployeeBo employeeBo = (IEmployeeBo) SpringBeanFactory
				.getBean("empBo");
		IAttdMachineBO attdMachineBO = (IAttdMachineBO) SpringBeanFactory
				.getBean("attdMachineBO");
		String machineIdArray[] = machineIdStr.split(";");
		List attdMachineList = attdMachineBO
				.findAttdMachineByIds(machineIdArray);
		if (attdMachineList == null)
			return StringUtil.message(msgParamError, new Object[0]);
		List employeeList = employeeBo.loadAll();
		String result = "";
		for (Iterator i$ = attdMachineList.iterator(); i$.hasNext();) {
			AttdMachine attdMachine = (AttdMachine) i$.next();
			if (!result.equals(""))
				result = (new StringBuilder()).append(result).append("")
						.toString();
			IMachineBO machineBO = MachineBOFactory.getMachineBO(attdMachine
					.getMacClass());
			int errorCode = machineBO.connectNet(attdMachine);
			if (errorCode != machineBO.getSuccCode()) {
				result = (new StringBuilder()).append(result).append(
						StringUtil.message(msgConnFail, new Object[] {
								attdMachine.getMacNo(),
								machineBO.getErrorByCode(errorCode) }))
						.toString();
			} else {
				logger.info(StringUtil.message(msgGetEmpStartLog,
						new Object[] { getCurrentEmp().getEmpName() }));
				String tempResult = compareTo(machineBO, employeeList,
						attdMachine, isClean);
				if (StringUtils.isEmpty(tempResult))
					result = (new StringBuilder()).append(result).append(
							StringUtil.message(msgEmpDeleteSucc,
									new Object[] { attdMachine.getMacNo() }))
							.toString();
				else
					result = (new StringBuilder()).append(result).append(
							tempResult).toString();
				logger.info(StringUtil.message(msgGetEmpEndLog,
						new Object[] { Integer.valueOf(employeeList.size()) }));
				machineBO.refreshData();
				machineBO.disConnectNet();
			}
		}

		return result;
	}

	private String compareTo(IMachineBO machineBO, List employeeList,
			AttdMachine machine, Integer isClean) {
		IAttdSyncRecordBO attdSyncRecordBO = (IAttdSyncRecordBO) SpringBeanFactory
				.getBean("attdSyncRecordBO");
		List recList = machineBO.getAllUserInfo();
		Map empMap = empListToMap(employeeList);
		List recordList = attdSyncRecordBO.getAttdSyncRecord(null, machine
				.getMacId());
		Map recordMap = new HashMap();
		if (recordList != null) {
			AttdSyncRecord record;
			for (Iterator i$ = recordList.iterator(); i$.hasNext(); recordMap
					.put((new StringBuilder()).append(
							record.getAsrEmpMachineNo()).append(
							record.getAsrAttdMachine().getMacId()).toString(),
							record))
				record = (AttdSyncRecord) i$.next();

		}
		List needUpdateRecord = new ArrayList();
		List needDeleteRecord = new ArrayList();
		String result = "";
		for (int i = 0; recList != null && i < recList.size(); i++) {
			AttdSyncRecord rec = (AttdSyncRecord) recList.get(i);
			Employee emp = (Employee) empMap.get(String.valueOf(rec
					.getAsrEmpMachineNo()));
			AttdSyncRecord record = (AttdSyncRecord) recordMap
					.get((new StringBuilder()).append(rec.getAsrEmpMachineNo())
							.append(machine.getMacId()).toString());
			int code = 0;
			if (emp == null && isClean.intValue() == 0) {
				code = machineBO.deleteUser(rec);
				if (code == machineBO.getSuccCode()) {
					if (record != null) {
						needDeleteRecord.add(record);
						recordMap.remove((new StringBuilder()).append(
								rec.getAsrEmpMachineNo()).append(
								machine.getMacId()).toString());
						continue;
					}
				} else {
					result = (new StringBuilder()).append(result).append(
							StringUtil.message(msgEmpDeleteFail, new Object[] {
									rec.getAsrEmp() == null ? rec
											.getAsrEmpMachineNo() : rec
											.getAsrEmp().getEmpName(),
									machine.getMacNo(),
									machineBO.getErrorByCode(code) }))
							.toString();
				}
			}
			if (emp == null
					&& (isClean.intValue() == 0
							&& code != machineBO.getSuccCode() || isClean
							.intValue() == 1)) {
				record = setSyncRecord(record, emp, machine, rec, recordMap,
						needUpdateRecord);
				record.setAsrSync(Integer.valueOf(0));
				continue;
			}
			record = setSyncRecord(record, emp, machine, rec, recordMap,
					needUpdateRecord);
			if (emp.getEmpStatus().intValue() == 0
					|| emp.getEmpShiftType().intValue() == 0) {
				record.setAsrSync(Integer.valueOf(0));
				continue;
			}
			int authLevel = rec.getAsrAuthLevel().intValue() != 0 ? 1 : 0;
			if (authLevel == record.getAsrAuthLevel().intValue()
					&& rec.getAsrEmpCardNo().equals(
							record.getAsrEmp().getEmpShiftNo())
					|| "0".equals(rec.getAsrEmpCardNo())
					&& StringUtils.isEmpty(record.getAsrEmp().getEmpShiftNo())) {
				record.setAsrSync(Integer.valueOf(1));
			} else {
				record.setAsrEmpCardNo(rec.getAsrEmpCardNo());
				record.setAsrSync(Integer.valueOf(0));
			}
		}

		if (!recordMap.isEmpty()) {
			AttdSyncRecord record;
			for (Iterator i$ = recordMap.keySet().iterator(); i$.hasNext(); needUpdateRecord
					.add(record)) {
				Object obj = (String) i$.next();
				record = (AttdSyncRecord) recordMap.get(obj);
				record.setAsrSync(Integer.valueOf(0));
			}

		}
		attdSyncRecordBO.insertDataList(needUpdateRecord);
		attdSyncRecordBO.deleteAttdSyncRecord(needDeleteRecord);
		return result;
	}

	private AttdSyncRecord setSyncRecord(AttdSyncRecord record, Employee emp,
			AttdMachine machine, AttdSyncRecord rec, Map recordMap,
			List needUpdateRecord) {
		if (record == null)
			record = new AttdSyncRecord();
		else
			recordMap.remove((new StringBuilder()).append(
					rec.getAsrEmpMachineNo()).append(machine.getMacId())
					.toString());
		if (emp != null)
			record.setAsrEmp(emp);
		else
			record.setAsrEmp(null);
		record.setAsrAttdMachine(machine);
		record.setAsrAuthLevel(Integer
				.valueOf(rec.getAsrAuthLevel().intValue() != 0 ? 1 : 0));
		record.setAsrEmpMachineNo(rec.getAsrEmpMachineNo());
		record.setAsrEmpCardNo(rec.getAsrEmpCardNo());
		needUpdateRecord.add(record);
		return record;
	}

	public String batchRead(String machineIdStr, String dateFromStr,
			String dateToStr) {
		if ("error".equals(DWRUtil.checkAuth("attdSyncRecordShow", "execute")))
			return StringUtil.message(msgNoAuth, new Object[0]);
		if (StringUtils.isEmpty(machineIdStr))
			return StringUtil.message(msgNoMacChoosed, new Object[0]);
		IAttdSyncRecordBO attdSyncRecordBO = (IAttdSyncRecordBO) SpringBeanFactory
				.getBean("attdSyncRecordBO");
		IAttdMachineBO attdMachineBO = (IAttdMachineBO) SpringBeanFactory
				.getBean("attdMachineBO");
		IAttdoriginaldataBO attdoriginaldataBO = (IAttdoriginaldataBO) SpringBeanFactory
				.getBean("attdoriginaldataBO");
		List recordList = attdSyncRecordBO.getAllRecordHasEmp();
		if (recordList == null)
			return StringUtil.message(msgNoCardDataToRead, new Object[0]);
		String machineIdArray[] = machineIdStr.split(";");
		List attdMachineList = attdMachineBO
				.findAttdMachineByIds(machineIdArray);
		if (attdMachineList == null) {
			return StringUtil.message(msgParamError, new Object[0]);
		} else {
			java.util.Date dateFrom = StringUtils.isEmpty(dateFromStr) ? null
					: DateUtil.parseDateByFormat(dateFromStr,
							"yyyy-MM-dd HH:mm");
			java.util.Date dateTo = StringUtils.isEmpty(dateToStr) ? null
					: DateUtil.parseDateByFormat(dateToStr, "yyyy-MM-dd HH:mm");
			String result = attdoriginaldataBO.batchReadRecord(recordList,
					attdMachineList, dateFrom, dateTo);
			return result;
		}
	}

	public String batchDelete(String recordIds) {
		if ("error".equals(DWRUtil.checkAuth("attdSyncRecordShow", "execute")))
			return StringUtil.message(msgNoAuth, new Object[0]);
		if (StringUtils.isEmpty(recordIds))
			return StringUtil.message(msgNoRecChoosed, new Object[0]);
		IAttdSyncRecordBO attdSyncRecordBO = (IAttdSyncRecordBO) SpringBeanFactory
				.getBean("attdSyncRecordBO");
		String recordIdArray[] = null;
		recordIdArray = recordIds.split(";");
		List recordList = attdSyncRecordBO.getAttdSyncRecord(recordIdArray);
		if (recordList == null)
			return StringUtil.message(msgParamError, new Object[0]);
		List attdMachineList = new ArrayList();
		Iterator it = recordList.iterator();
		do {
			if (!it.hasNext())
				break;
			AttdSyncRecord record = (AttdSyncRecord) it.next();
			if (record.getAsrAttdMachine() != null
					&& !attdMachineList.contains(record.getAsrAttdMachine()))
				attdMachineList.add(record.getAsrAttdMachine());
		} while (true);
		if (attdMachineList.isEmpty())
			return StringUtil.message(msgParamError, new Object[0]);
		String returnStr = "";
		List machineRecord = new ArrayList();
		List deleteRecord = new ArrayList();
		List updateRecord = new ArrayList();
		String succMsg = msgSuccMsgStart;
		String errorMsg = msgErrorMsgStart;
		for (it = attdMachineList.iterator(); it.hasNext();) {
			AttdMachine attdMachine = (AttdMachine) it.next();
			machineRecord.clear();
			Iterator i$ = recordList.iterator();
			do {
				if (!i$.hasNext())
					break;
				AttdSyncRecord record = (AttdSyncRecord) i$.next();
				if (record.getAsrAttdMachine().getMacId().equals(
						attdMachine.getMacId()))
					machineRecord.add(record);
			} while (true);
			IMachineBO machineBO = MachineBOFactory.getMachineBO(attdMachine
					.getMacClass());
			int errorCode = machineBO.connectNet(attdMachine);
			if (errorCode != machineBO.getSuccCode()) {
				returnStr = (new StringBuilder()).append(returnStr).append(
						StringUtil.message(msgConnFail, new Object[] {
								attdMachine.getMacNo(),
								machineBO.getErrorByCode(errorCode) }))
						.toString();
			} else {
				logger.info(StringUtil.message(msgDeleteEmpStartLog,
						new Object[] { getCurrentEmp().getEmpName() }));
				for (it = machineRecord.iterator(); it.hasNext();) {
					AttdSyncRecord rec = (AttdSyncRecord) it.next();
					errorCode = machineBO.deleteUser(rec);
					if (errorCode != machineBO.getSuccCode()) {
						rec.setAsrSync(Integer.valueOf(0));
						updateRecord.add(rec);
						errorMsg = (new StringBuilder())
								.append(errorMsg)
								.append(
										StringUtil
												.message(
														msgDeleteEmpFail,
														new Object[] {
																attdMachine
																		.getMacNo(),
																rec.getAsrEmp() == null ? rec
																		.getAsrEmpMachineNo()
																		: rec
																				.getAsrEmp()
																				.getEmpName(),
																machineBO
																		.getErrorByCode(errorCode) }))
								.toString();
					} else {
						deleteRecord.add(rec);
						succMsg = (new StringBuilder())
								.append(succMsg)
								.append(
										StringUtil
												.message(
														msgDeleteEmpSucc,
														new Object[] {
																attdMachine
																		.getMacNo(),
																rec.getAsrEmp() == null ? rec
																		.getAsrEmpMachineNo()
																		: rec
																				.getAsrEmp()
																				.getEmpName() }))
								.toString();
					}
				}

				machineBO.refreshData();
				machineBO.disConnectNet();
			}
		}

		if (deleteRecord.size() > 0)
			attdSyncRecordBO.deleteAttdSyncRecord(deleteRecord);
		if (updateRecord.size() > 0)
			attdSyncRecordBO.insertDataList(updateRecord);
		if (!succMsg.equals(msgSuccMsgStart))
			returnStr = (new StringBuilder()).append(returnStr).append(succMsg)
					.toString();
		if (!errorMsg.equals(msgErrorMsgStart))
			returnStr = (new StringBuilder()).append(returnStr)
					.append(errorMsg).toString();
		logger.info(StringUtil.message(msgDeleteEmpEndLog,
				new Object[] { Integer.valueOf(recordList.size()) }));
		return returnStr;
	}

	public String linkMachine(String macIP, String macPort, String macPassword,
			String macHasFinger, String machineClass) {
		if (StringUtils.isEmpty(macIP))
			return StringUtil.message(msgMacIp, new Object[0]);
		if (!StringUtil.isNumber(macPort))
			return StringUtil.message(msgMacPort, new Object[0]);
		if (!StringUtil.isNumber(macPassword))
			return StringUtil.message(msgMacPassword, new Object[0]);
		AttdMachine attdMachine = new AttdMachine();
		attdMachine.setMacIP(macIP);
		attdMachine.setMacPort(macPort);
		attdMachine.setMacPassword(Integer.valueOf(Integer
				.parseInt(macPassword)));
		attdMachine.setMacHasFinger(Integer.valueOf(Integer
				.parseInt(macHasFinger)));
		attdMachine.setMacClass(machineClass);
		IMachineBO machineBO = MachineBOFactory.getMachineBO(attdMachine
				.getMacClass());
		int errorCode = machineBO.connectNet(attdMachine);
		if (errorCode != machineBO.getSuccCode()) {
			return StringUtil.message(msgConnFail,
					new Object[] { attdMachine.getMacNo(),
							machineBO.getErrorByCode(errorCode) });
		} else {
			machineBO.disConnectNet();
			return "success";
		}
	}

	private Map empListToMap(List list) {
		Map map = new HashMap();
		Employee emp;
		for (Iterator i$ = list.iterator(); i$.hasNext(); map.put(String
				.valueOf(emp.getEmpMachineNo()), emp))
			emp = (Employee) i$.next();

		return map;
	}

	public String copyUserFingerToMachine(String asrIds, String machineIds) {
		IAttdMachineBO attdMachineBO = (IAttdMachineBO) SpringBeanFactory
				.getBean("attdMachineBO");
		IAttdSyncRecordBO attdSyncRecordBO = (IAttdSyncRecordBO) SpringBeanFactory
				.getBean("attdSyncRecordBO");
		String recordIds[] = asrIds.split(",");
		List syncRecords = attdSyncRecordBO.getAttdSyncRecord(recordIds);
		if (syncRecords.size() == 0)
			return StringUtil.message(msgParamError, new Object[0]);
		AttdMachine fromMachine = ((AttdSyncRecord) syncRecords.get(0))
				.getAsrAttdMachine();
		if (fromMachine.getMacHasFinger().intValue() != 1)
			return StringUtil.message(msgCopyMachineFinger,
					new Object[] { fromMachine.getMacNo() });
		for (int i = 0; i < syncRecords.size(); i++) {
			AttdMachine tempMachine = ((AttdSyncRecord) syncRecords.get(i))
					.getAsrAttdMachine();
			if (!tempMachine.getMacId().equals(fromMachine.getMacId()))
				return StringUtil.message(msgCopyOneMachine, new Object[0]);
		}

		String machineIdArray[] = machineIds.split(",");
		List attdMachineList = attdMachineBO
				.findAttdMachineByIds(machineIdArray);
		if (attdMachineList == null)
			return StringUtil.message(msgParamError, new Object[0]);
		for (int i = 0; i < attdMachineList.size(); i++) {
			AttdMachine tempMachine = (AttdMachine) attdMachineList.get(i);
			if (tempMachine.getMacHasFinger().intValue() != 1)
				return StringUtil.message(msgCopyMachineFinger,
						new Object[] { fromMachine.getMacNo() });
			if (tempMachine.getMacId().equals(fromMachine.getMacId()))
				return StringUtil.message(msgFromMachineError, new Object[0]);
		}

		Map empMap = new HashMap();
		List recList = new ArrayList();
		IMachineBO machineBO = MachineBOFactory.getMachineBO(fromMachine
				.getMacClass());
		int errorCode = machineBO.connectNet(fromMachine);
		if (errorCode != machineBO.getSuccCode())
			return StringUtil.message(msgConnFail,
					new Object[] { fromMachine.getMacNo(),
							machineBO.getErrorByCode(errorCode) });
		for (int i = 0; syncRecords != null && i < syncRecords.size(); i++) {
			AttdSyncRecord asrTemp = (AttdSyncRecord) syncRecords.get(i);
			empMap.put(asrTemp.getAsrEmp().getEmpMachineNo(), asrTemp
					.getAsrEmp());
			AttdSyncRecord rec = machineBO.getOneUserInfo(asrTemp.getAsrEmp()
					.getEmpMachineNo().intValue());
			if (rec != null)
				recList.add(rec);
		}

		machineBO.disConnectNet();
		if (recList.size() == 0)
			return StringUtil.message(msgCopyNoRec, new Object[] { fromMachine
					.getMacNo() });
		String returnStr = "";
		for (Iterator i$ = attdMachineList.iterator(); i$.hasNext();) {
			AttdMachine machine = (AttdMachine) i$.next();
			machineBO = MachineBOFactory
					.getMachineBO(fromMachine.getMacClass());
			errorCode = machineBO.connectNet(machine);
			if (errorCode != machineBO.getSuccCode()) {
				returnStr = (new StringBuilder()).append(returnStr).append(
						StringUtil.message(msgConnFail, new Object[] {
								machine.getMacNo(),
								machineBO.getErrorByCode(errorCode) }))
						.toString();
			} else {
				for (int i = 0; i < recList.size(); i++) {
					AttdSyncRecord rec = (AttdSyncRecord) recList.get(i);
					Employee emp = (Employee) empMap.get(rec
							.getAsrEmpMachineNo());
					errorCode = machineBO.addOrModifyUser(rec);
					if (errorCode != machineBO.getSuccCode())
						returnStr = (new StringBuilder())
								.append(returnStr)
								.append(
										StringUtil
												.message(
														msgEmpSyncFail,
														new Object[] {
																emp
																		.getEmpDistinctNo(),
																emp
																		.getEmpName(),
																machine
																		.getMacNo(),
																machineBO
																		.getErrorByCode(errorCode) }))
								.toString();
					else
						returnStr = (new StringBuilder()).append(returnStr)
								.append(
										StringUtil.message(msgEmpSyncSucc,
												new Object[] {
														emp.getEmpDistinctNo(),
														emp.getEmpName(),
														machine.getMacNo() }))
								.toString();
				}

				machineBO.disConnectNet();
			}
		}

		return returnStr;
	}

	public String syncMachineTime(String macIP, String macPort,
			String macPassword, String macHasFinger, String machineClass) {
		if (StringUtils.isEmpty(machineClass))
			return StringUtil.message(msgMacClass, new Object[0]);
		if (StringUtils.isEmpty(macIP))
			return StringUtil.message(msgMacIp, new Object[0]);
		if (!StringUtil.isNumber(macPort))
			return StringUtil.message(msgMacPort, new Object[0]);
		if (!StringUtil.isNumber(macPassword))
			return StringUtil.message(msgMacPassword, new Object[0]);
		AttdMachine attdMachine = new AttdMachine();
		attdMachine.setMacIP(macIP);
		attdMachine.setMacPort(macPort);
		attdMachine.setMacPassword(Integer.valueOf(Integer
				.parseInt(macPassword)));
		attdMachine.setMacHasFinger(Integer.valueOf(Integer
				.parseInt(macHasFinger)));
		attdMachine.setMacClass(machineClass);
		IMachineBO machineBO = MachineBOFactory.getMachineBO(attdMachine
				.getMacClass());
		int errorCode = machineBO.connectNet(attdMachine);
		if (errorCode != machineBO.getSuccCode())
			return StringUtil.message(msgConnFail,
					new Object[] { attdMachine.getMacNo(),
							machineBO.getErrorByCode(errorCode) });
		if (!machineBO.syncMachineTime()) {
			return StringUtil.message(msgSyncTimeFail, new Object[0]);
		} else {
			machineBO.disConnectNet();
			return StringUtil.message(msgSyncTimeSucc, new Object[0]);
		}
	}

	static Logger logger = Logger.getLogger(DwrSyncAttdMachine.class);
	String msgNoAuth;
	String msgNoEmpChoosed;
	String msgNoMacChoosed;
	String msgParamError;
	String msgSuccMsgStart;
	String msgErrorMsgStart;
	String msgConnFail;
	String msgSyncEmpStartLog;
	String msgSyncEmpEndLog;
	String msgEmpCardNoNull;
	String msgEmpCardNoRepeat;
	String msgEmpSyncFail;
	String msgEmpSyncSucc;
	String msgGetEmpStartLog;
	String msgGetEmpEndLog;
	String msgEmpDeleteFail;
	String msgEmpDeleteSucc;
	String msgNoCardDataToRead;
	String msgNoRecChoosed;
	String msgDeleteEmpStartLog;
	String msgDeleteEmpEndLog;
	String msgDeleteEmpFail;
	String msgDeleteEmpSucc;
	String msgMacClass;
	String msgMacIp;
	String msgMacPort;
	String msgMacPassword;
	String msgCopyOneMachine;
	String msgFromMachineError;
	String msgCopyMachineFinger;
	String msgCopyNoRec;
	String msgSyncTimeFail;
	String msgSyncTimeSucc;

}
